<script setup lang="ts">
import { ref, onMounted } from "vue";
import { Overlay, ConfigProvider } from "vant";
import { useI18n } from "vue-i18n";
import { FiltrationFun } from "@/utils";
import { InstallGuideConfigInterval } from "@/constant";
const props = defineProps({
  androidInstallType: {
    type: Number,
    default: 1, // 安卓弹框安装类型 1=pwa(快捷安装) 2=apk(需自己安装) 3=pwa(优先)+apk 1
  },
  apkUrl: {
    type: String,
    default: "",
  },
});
const overlayShow = ref(false);
const { t } = useI18n();
const seconds = ref(0);
//1安卓 2苹果
const deviceType = ref(0);
//判断是什么环境
const matrix = ref("Browser");
onMounted(() => {
  matrix.value = FiltrationFun.detectEnvironment();
  deviceType.value = FiltrationFun.getDeviceType() === "Android" ? 1 : 2;
  if (props.androidInstallType == 2) {
    deviceType.value = 3;
  }
  if (addPwaloadedType.value == 1) {
    openNewHtmlType.value = 1;
    seconds.value = 100;
    downPwaApk();
  }
});
const secondsAdd = () => {
  const settime = setInterval(() => {
    if (seconds.value === 100) {
      clearInterval(settime);
      return false;
    }
    seconds.value = seconds.value + 1;
  }, 100);
};
//打开页面下载pwa的页面
const closefun = () => {
  if (addPwaloadedType.value === 0) {
    overlayShow.value = true;
    secondsAdd();
    seconds.value = 0;
    openNewHtmlType.value = 0;
  } else {
    downPwaApk();
  }
};
//在pwa中打开新的浏览器页面
const pwaOpenPage = () => {
  const currentUrl = window.location.href;
  const index = currentUrl.indexOf("#");
  // 截取从 0 开始到指定字符串下标的部分
  const httpUrl = currentUrl.substring(0, index);
  window.open(httpUrl + "?sdmode=2");
  overlayShow.value = false;
};
//打开新页面的安装好apk，pwa的状态
const openNewHtmlType = ref(0);
//有加载过
const addPwaloadedType = ref(0);
//下载pwa和apk
const downPwaApk = () => {
  if (matrix.value === "pwa") {
    pwaOpenPage();
  }
  if (InstallGuideConfigInterval.deferredPrompt) {
    //安装提示
    InstallGuideConfigInterval.deferredPrompt.prompt();
    addPwaloadedType.value = 1;
    // 获取用户的选择
    InstallGuideConfigInterval.deferredPrompt.userChoice.then(choiceResult => {
      //choiceResult.outcome === "accepted" ? "用户接受" : "用户拒绝"
      if (choiceResult.outcome === "accepted") {
        if (props.androidInstallType === 3) {
          deviceType.value = 3;
          FiltrationFun.setReportEvent("CustomizeProduct");
        }
      } else {
        if (props.androidInstallType === 3) {
          //拒绝
          deviceType.value = 3;
        }
      }
    });
  }
};
defineExpose({ closefun });
//下载apk的方法
const donwApk = () => {
  //拒绝就直接下载apk
  const link = document.createElement("a");
  link.href = props.apkUrl; // APK 文件链接
  link.download = "br0011.apk"; // 文件名
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
  FiltrationFun.setReportEvent("CustomizeProduct");
  overlayShow.value = false;
  seconds.value = 0;
  openNewHtmlType.value = 0;
};
//下载app弹窗取消的
const clotureDownAPP = () => {
  deviceType.value = 1;
  overlayShow.value = false;
  seconds.value = 0;
  openNewHtmlType.value = 0;
};
</script>
<template>
  <!-- 下载pwa和apk的 -->
  <ConfigProvider>
    <Overlay
      :show="overlayShow"
      @click="overlayShow = false"
      z-index="3000"
      style="background-color: #000000b3"
    >
      <div class="wrapper">
        <div class="block" v-if="deviceType === 1">
          <div class="title-frame df ai-center jc-center">
            <img class="close-img" src="@/image/new_img/home/ic_raid_install.png" />
            <div class="title-text">{{ t("home.rapidInstall") }}</div>
          </div>
          <div
            class="install-now-loading df ai-center jc-center"
            v-show="seconds < 100 && openNewHtmlType === 0"
          >
            <div class="loading-ci-box"></div>
            <div class="percentage-frame">{{ seconds }}%</div>
          </div>
          <div class="donw-frame">
            <div
              class="title-content df ai-center jc-center"
              v-show="seconds === 100 || openNewHtmlType === 1"
            >
              <img class="actived-img" src="@/image/new_img/home/ic_actived.png" />
              <span>Effective</span>
            </div>
            <div class="progress-container" v-if="seconds < 100 && openNewHtmlType === 1">
              <div class="progress-bar" :style="{ width: seconds + '%' }"></div>
              <div class="percentage-frame">{{ seconds }}%</div>
            </div>
            <div
              class="down-frame df ai-center jc-center"
              v-if="seconds === 100 && openNewHtmlType === 1"
              @click.stop="pwaOpenPage"
            >
              {{ t("home.playNow") }}
            </div>
            <div
              class="down-frame df ai-center jc-center"
              @click.stop="downPwaApk"
              v-if="
                matrix !== 'pwa' && matrix !== 'apk' && openNewHtmlType === 0 && seconds === 100
              "
            >
              Install Now
            </div>
          </div>
        </div>
        <div v-if="deviceType === 3" class="donwAPK-frame">
          <div class="sdapk-bg">
            <img class="sdapk-bg-img" src="@/image/new_img/home/popup_bg.png" />
            <img class="popup-icon-img" src="@/image/new_img/home/popup_icon.png" />
          </div>
          <div class="title-frame df ai-center jc-center">
            {{ t("home.downAPKtitle") }}
          </div>
          <div class="df ai-center jc-center bottom-frame">
            <div class="cancel-frame" @click.stop="clotureDownAPP">
              {{ t("home.cancel") }}
            </div>
            <div class="confirm-frame" @click.stop="donwApk">
              {{ t("home.confirm") }}
            </div>
          </div>
        </div>
      </div>
    </Overlay>
  </ConfigProvider>
</template>
<style lang="less" scoped>
.wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  width: 100%;
  position: relative;
  left: 0;
  top: 0;
}

.block {
  width: 622px;
  height: 430px;
  background-color: #ffffff;
  border-radius: 20px;
  .title-frame {
    width: 100%;
    height: 82px;
    line-height: 82px;
    padding-top: 62px;
    margin-bottom: 20px;
    .close-img {
      width: 56px;
      height: 66px;
      margin-right: 14px;
    }
    .title-text {
      font-size: 54px;
      color: #666;
    }
  }
  .install-now-loading {
    width: 200px;
    height: 200px;
    margin: auto;
    position: relative;
    left: 0;
    top: 0;
    .percentage-frame {
      font-size: 32px;
      color: #028760;
      font-weight: 600;
    }
  }
  .loading-ci-box {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
    border-color: transparent #028760;
    border-style: solid;
    border-width: 5px;
    border-radius: 50%;
    -webkit-animation: turnbbb 0.65s linear infinite;
    animation: turnbbb 0.65s linear infinite;
    -webkit-transform-origin: 50% 50%;
    transform-origin: 50% 50%;
  }
  @-webkit-keyframes turnbbb {
    from {
      -webkit-transform: rotate(0deg);
    }
    to {
      -webkit-transform: rotate(-360deg);
    }
  }

  @keyframes turnbbb {
    from {
      transform: rotate(0deg);
    }
    to {
      transform: rotate(-360deg);
    }
  }
  .donw-frame {
    width: 100%;
    height: auto;
  }
  .title-content {
    width: 248px;
    height: 72px;
    border-radius: 40px;
    background-color: #77fa494d;
    margin-bottom: 48px;
    margin: auto;
    img {
      margin-right: 10px;
      width: 24px;
      height: auto;
    }
    span {
      color: #01875f;
      font-size: 32px;
      font-weight: 700;
    }
  }
  .down-frame {
    width: 526px;
    font-weight: 500;
    height: 90px;
    color: #ffffff;
    background-color: #01875f;
    border-radius: 90px;
    margin: auto;
    margin-top: 48px;
    font-size: 30px;
    user-select: none; /* 禁用文本选择 */
  }
}
.donwPwa-frame {
  width: 100%;
  height: 810px;
  position: absolute;
  left: 0;
  bottom: 60px;
  text-align: center;
}
.cloture-frame {
  width: 120px;
  height: 120px;
  position: absolute;
  right: 40px;
  top: 0;
  z-index: 10;
}
.hometips-img {
  width: 660px;
  height: 810px;
  margin: auto;
}
.donwAPK-frame {
  width: 630px;
  height: 488px;
  border-radius: 36px;
  background-color: #ffffff;
  .sdapk-bg {
    position: relative;
    left: 0;
    top: 0;
    width: 100%;
    height: 184px;
    .sdapk-bg-img {
      width: 100%;
      height: 100%;
    }
    .popup-icon-img {
      width: 100%;
      height: auto;
      position: absolute;
      left: 0;
      bottom: 0;
    }
  }
  .title-frame {
    width: 100%;
    height: 150px;
    margin-bottom: 10px;
    font-weight: 400;
    font-size: 28px;
  }
  .bottom-frame {
    width: 100%;
    height: auto;
  }
  .cancel-frame {
    border: 1px solid #fba531;
    color: #111;
    font-weight: 400;
    font-size: 26px;
    width: 226px;
    height: 66px;
    border-radius: 12px;
    text-align: center;
    line-height: 66px;
    margin-right: 40px;
  }
  .confirm-frame {
    width: 226px;
    height: 66px;
    border-radius: 12px;
    color: #ffffff;
    font-weight: 400;
    font-size: 26px;
    background-color: #fba531;
    text-align: center;
    line-height: 66px;
  }
}
.progress-container {
  width: 80%;
  height: 90px;
  background-color: #e0e0e0;
  border-radius: 10px;
  overflow: hidden;
  margin-bottom: 1rem;
  border-radius: 90px;
  border: 2px solid rgb(156 221 128);
  margin: auto;
  position: relative;
  left: 0;
  top: 0;
  margin-top: 40px;
  .percentage-frame {
    font-size: 32px;
    color: #fba531;
    font-weight: 600;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
  .progress-bar {
    height: 100%;
    background-color: #028760;
    transition: width 0.1s ease;
  }
}
</style>
